package net.ncraft.mc.easyframework.aop.core;

import lombok.Getter;
import lombok.Setter;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class JoinPoint{

    private Object targetInstance;
    @Getter
    private Method originalMethod;
    @Getter
    private MethodProxy method;
    @Getter
    private Object[] args;
    @Getter
    private Object aspectInstance;
    @Getter
    private Method aspectMethod;
    @Setter
    private JoinPoint next;

    public JoinPoint(Object targetInstance, Method originalMethod, MethodProxy method, Object[] args, Object aspectInstance, Method aspectMethod) {
        this.targetInstance = targetInstance;
        this.originalMethod = originalMethod;
        this.method = method;
        this.args = args;
        this.aspectInstance = aspectInstance;
        this.aspectMethod = aspectMethod;
    }

    public Object proceed() throws Throwable {
        if (next!=null) {
            return next.aspectMethod.invoke(next.aspectInstance, next);
        }
        return method.invoke(targetInstance, args);
    }

    public Object getTarget() {
        return this.targetInstance;
    }

}
